<template>
	<view>
		<!-- <view class="head-top">
			<view style="height: 4vh">
			</view>
			<view class="head-top-row">
				<view class="head-top-row-right" @click="Hui">
					<view class="triangle-facing-left"></view> 返回 </view>
						<view class="head-top-row-left">
							<view class="head-top-row-left-text">
								物料接收
							</view>
						</view>
				</view>
			</view> -->
			<view class="head-body">
				<uni-section title="工单信息" type="line" titleFontSize="20px">
					<uni-row class="demo-uni-row" :width="nvueWidth">
						<uni-col :span="4">
							<view class="gd-text">MO号:</view>
						</uni-col>
						<uni-col :span="14">
							<uni-easyinput trim="all" v-model="mo" placeholder=""></uni-easyinput>
						</uni-col>
						<uni-col :span="6">
							<yt-scanCode btnName="扫码" style="margin-left: 18%;margin-top: 3px;"
								@getScanCode="OnScanningCode"></yt-scanCode>
						</uni-col>
					</uni-row>
				</uni-section>
				<view style="border-top: 1px solid #cecece; margin-top: 30px;">
				</view>
				<uni-section title="物料信息" type="line" titleFontSize="20px">
					<template v-slot:right>
						<uni-row>
							<uni-col :span="4.5">
								<button type="primary" style="margin-top: 10px;" size="mini" @click="save">保存</button>
							</uni-col>
						</uni-row>
					</template>
					<uni-card>
						<view style="height:55vh">
							<zb-table :columns="column" :stripe="true" :border="true" :data="data"
								:cell-style="cellStyle" @dele="dele"></zb-table>
						</view>
					</uni-card>
				</uni-section>
			</view>
			<uni-popup ref="Dialog" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" title="物料信息" :before-close="true"
					@confirm="dialogInputConfirm" @close="dialogInputConfirmclose">
					<uni-forms ref="customForm" :rules="customRules" labelWidth="90px" :modelValue="customFormData">
						<uni-forms-item label="领料单号：" name="Material_Requisition">
							<uni-easyinput v-model="customFormData.Material_Requisition" placeholder=""
								:disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="工序号：" name="OpNum">
							<uni-easyinput v-model="customFormData.OpNum" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="料号：" name="PartNumber">
							<uni-easyinput v-model="customFormData.PartNumber" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="发料数量：" name="IssueQty">
							<uni-easyinput v-model="customFormData.IssueQty" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="需求数量：" name="DemandQuantity">
							<uni-easyinput v-model="customFormData.DemandQuantity" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="实收数量：" name="lastreciveNum">
							<uni-easyinput v-model="customFormData.lastreciveNum" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="核对数量：" name="reciveNum">
							<uni-easyinput v-model="customFormData.reciveNum" placeholder="" />
						</uni-forms-item>
					</uni-forms>
				</uni-popup-dialog>
			</uni-popup>
			<sl-pda-employ :config="pdaConfig"></sl-pda-employ>
		</view>
</template>

<script>
	import {
		MaterialReceivingQuery,
		MaterialInformationQuery,
		QueryIssueInfoByIssueDocNo,
		MaterialInformationSave,
		NewQuery
	} from '@/api/system/MaterialReceiving'
	import slPdaEmploy, {
		initPdaScan,
		manualTrigScan,
		onUnloadPdaScan,
		BroadcastConfig
	} from '../../uni_modules/sl-pdaEmploy'
	export default {
		data() {
			return {
				pdaConfig: { // 可以抽离出去管理这个配置
					BroadcastAction: 'nantong',
					BroadcastLabelKey: 'data',
					//BroadcastTriglKey: 'com.scan.onStartScan',
				},
				nvueWidth: uni.getWindowInfo().screenWidth,
				mo: "",
				RowAll: "",
				data: [],
				ScanMaterialsText: "",
				customRules: {
					PartNumber: {
						rules: [{
							required: true,
							errorMessage: '料号不能为空'
						}]
					},
					reciveNum: {
						rules: [{
							required: true,
							errorMessage: '核对数量不能为空'
						}, {
							format: 'number',
							errorMessage: '核对数量只能输入数字'
						}]
					}
				},
				packagingCode: "",
				customFormData: {
					Material_Requisition: "",
					OpNum: "",
					PartNumber: "",
					DemandQuantity: "",
					reciveNum: "",
					lastreciveNum: "",
					IssueQty: "",
					jobId: "",
					mateId: "",
					mateCode: "",
					mateName: "",
					mateDesignNumber: "",
					mateType: "",
					reciveNum: "",
					reciveId: "",
					reviceCode: "",
					reciveName: "",
					packagingCode: "",
					reqId: "",
				},
				column: [{
						label: '序号',
						type: 'index',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 10 * 1.1
					},
					{
						name: 'PartNumber',
						label: '料号',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 100 * 22
					},
					{
						name: 'OpNum',
						label: '工序',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 100 * 13
					},
					{
						name: 'DemandQuantity',
						align: "center",
						label: '需求数量',
						width: uni.getWindowInfo().screenWidth / 100 * 17
					},
					{
						name: 'reciveNum',
						label: '核对数量',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 100 * 17
					},
					{
						name: 'operation',
						type: 'operation',
						label: '操作',
						width: uni.getWindowInfo().screenWidth / 100 * 13,
						renders: [{
							name: '删除',
							class: 'edit',
							type: "primary",
							func: 'dele',
						}, ]
					},
				]
			}
		},
		components: {
			slPdaEmploy
		},
		onLoad() {
			initPdaScan(code => {
				console.log(code)
				this.clear();
				if (JSON.parse(code).Material_Requisition === undefined) {
					uni.showToast({
						title: "无效二维码",
						icon: "error",
						duration: 2000
					})
				} else {
					this.packagingCode = JSON.stringify(code);
					this.OnQueryIssueInfoByIssueDocNo(JSON.parse(code).Material_Requisition, JSON.parse(code).Material_Number, JSON.parse(code).QTY);
				}
			})
		},
		onUnload() {
			onUnloadPdaScan()
		},
		methods: {
			dele(ite, index) {
				this.data.splice(index, 1)
			},
			cellStyle({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				var arrayA = [];
				var arrayB = this.RowAll.substring(0, this.RowAll.length - 1).split(",");
				for (var i = 0; i < this.data.length; i++) {
					arrayA.push("" + this.data[i].reqId)
				}
				const equalIndexes = [];
				arrayA.forEach((element, index) => {
					if (arrayB.includes(element)) {
						equalIndexes.push(index);
					}
				});
				//console.log(arrayA, arrayB, equalIndexes)
				for (var i = 0; i < equalIndexes.length; i++) {
					if (rowIndex === equalIndexes[i]) {
						return {
							color: 'red'
						}
					}
				}

			},
			save() {
				const table = this.data;
				if (this.data.length > 0) {
					MaterialInformationSave(this.data).then(res => {
						console.log("物料保存", res)
						if (res.code === 200) {
							this.RowAll = "";
							this.$modal.msgSuccess("保存成功");
							this.data = [];
						} else {
							uni.showToast({
								title: "同时操作物料，超出最大数量",
								icon: "none",
								mask: true,
							})
						}
					}).catch(error => {
						reject(error)
					})
				} else {
					this.$modal.msgError("请添加物料")
				}
			},
			OnScanningCode(res) {
				this.clear();
				//console.log(JSON.parse(res).Material_Requisition)
				if (JSON.parse(res).Material_Requisition === undefined) {
					uni.showToast({
						title: "无效二维码",
						icon: "error",
						duration: 2000
					})
				} else {
					this.packagingCode = JSON.stringify(res);
					this.OnQueryIssueInfoByIssueDocNo(JSON.parse(code).Material_Requisition, JSON.parse(code).Material_Number, JSON.parse(code).QTY);
					
				}
			},
			OnQueryIssueInfoByIssueDocNo(Material_Requisition, Material_Number,QTY) {
				QueryIssueInfoByIssueDocNo(Material_Requisition).then(res => {
					// console.log(res)
					if (res.status === 200) {
						if (res.data.length > 0) {
							var a=0;
							// let IssueQty =0;
							for (var i = 0; i < res.data.length; i++) {
								if (res.data[i].ItemCode === Material_Number) {
									 a=1;
									console.log(res.data)
									// IssueQty =  IssueQty + res.data[i].IssueQty
									this.mo = res.data[i].MoDocNo;
									var MoDocNo = res.data[i].MoDocNo;
									var ItemCode = res.data[i].ItemCode;
									var OpNum = res.data[i].OpNum;
									this.customFormData.IssueQty = QTY;
									NewQuery(MoDocNo, ItemCode, OpNum).then(res => {
										console.log(res)
										if (res.data.length > 0) {
											let sum = 0;
											let reciveNum = 0;
											let planNum = 0;
											for (var k = 0; k < res.data.length; k++) {
											for (var i = 0; i < this.data.length; i++) {
												if (this.data[i].reqId === res.data[k].id) {
													sum = sum + this.data[i].reciveNum * 1;
												}
											}
											reciveNum = reciveNum + res.data[k].reciveNum * 1 ;
											planNum = planNum + res.data[k].planNum * 1;
											this.customFormData.Material_Requisition =Material_Requisition;
											this.customFormData.outBoundId =Material_Requisition;
											this.customFormData.username =this.$store.state.user.userAll.userName;
											this.customFormData.OpNum = OpNum;
											this.customFormData.PartNumber = res.data[k].mateCode;
											this.customFormData.DemandQuantity = planNum;
											this.customFormData.reciveNum = planNum-reciveNum- sum * 1>QTY?QTY- sum * 1:planNum- reciveNum- sum * 1;
											this.customFormData.lastreciveNum = reciveNum;
											this.customFormData.jobId = res.data[k].jobId;
											this.customFormData.mateId = res.data[k].mateId;
											this.customFormData.mateCode = res.data[k].mateCode;
											this.customFormData.mateName = res.data[k].mateName;
											this.customFormData.mateDesignNumber = res.data[k].mateDesignNumber;
											this.customFormData.mateType = res.data[k].matetType;
											this.customFormData.reciveId = this.$store.state.user.userAll.userId,
											this.customFormData.reviceCode = this.$store.state.user.userAll.userCode,
											this.customFormData.reciveName = this.$store.state.user.userAll.userName,
											this.customFormData.packagingCode = this.packagingCode,
											this.customFormData.reqId = res.data[k].id;
											}
											this.$refs.Dialog.open();
										} else {
											uni.showToast({
												title: "未查询到" + OpNum + "工序下物料:" + ItemCode,
												icon: "none",
												duration: 5000
											})
										}
									}).catch(error => {
										reject(error)
									})
								}else{
									if(i== res.data.length-1&&a==0){
										uni.showToast({
											title: "未查询匹配到标签二维码中内容,"+Material_Number+",请联系信息化处理",
											icon: "none",
											duration: 5000
										})
									}
								}
							}
						} else {
							uni.showToast({
								title: "U9未查询到：" + Material_Requisition,
								icon: "none",
								duration: 4000
							})
						}
					} else {
						uni.showToast({
							title: "U9报错：" + res.message,
							icon: "none",
							duration: 4000
						})
					}
				}).catch(error => {
					reject(error)
				})
			},
			dialogInputConfirmclose() {
				this.$refs.Dialog.close();
				this.clear();
			},
			dialogInputConfirm() {
				this.$refs["customForm"].validate().then(res => {
					let sum = 0;
					for (var i = 0; i < this.data.length; i++) {
						if (this.data[i].reqId === this.customFormData.reqId) {
							sum = sum + this.data[i].reciveNum * 1;
						}
					}
					if (this.customFormData.reciveNum * 1 + this.customFormData.lastreciveNum * 1 + sum >
						this.customFormData.DemandQuantity * 1) {
						this.$modal.msgError(this.customFormData.reciveNum * 1 + sum + this.customFormData
							.lastreciveNum * 1 + "大于需求数量")
					} else if(this.customFormData.reciveNum * 1===0){
						this.$modal.msgError("核对数量为零")
					} else{
						this.data.push(this.customFormData);
						this.clear();
						this.$refs.Dialog.close();
					}
				}).catch(err => {
					//console.log('err', err);
				})
			},
			Hui() {
				uni.navigateBack();
			},
			clear() {
				//this.mo = "";
				this.packagingCode = "";
				this.customFormData = {
					IssueQty: "",
					Material_Requisition: "",
					OpNum: "",
					PartNumber: "",
					DemandQuantity: "",
					reciveNum: "",
					lastreciveNum: "",
					jobId: "",
					mateId: "",
					mateCode: "",
					mateName: "",
					mateDesignNumber: "",
					mateType: "",
					reciveNum: "",
					reciveId: "",
					reviceCode: "",
					reciveName: "",
					reciveDate: "",
					packagingCode: "",
					reqId: "",
				};
			}
		}
	}
</script>

<style lang="scss">
	.head-top {
		height: 10vh;
	}

	.head-top-row {
		background-color: #007AFF;
		display: flex;
		height: 6vh;
	}

	.head-top-row-left {
		padding-top: 0.5vh;
		font-size: 19px;
		font-weight: 500;
		width: 86%;
	}

	.head-top-row-left-text {
		padding-left: 30%;
		color: #ffffff;
	}

	.head-top-row-right {
		color: #ffffff;
		margin-left: 2%;
		width: 14%;
		font-size: 17px;
		font-weight: 500;
		padding-top: 1vh;
	}

	.head-body {
		background-color: #ffffff;
		height: 90vh;
	}

	.demo-uni-row {
		padding-left: 10px;
		margin-top: 6px;
	}

	.gd-text {
		font-size: 17px;
		margin-top: 4px;
		float: right;
		margin-right: 6px;
	}

	::v-deep {
		.uni-card {
			margin: 1% !important;
			padding: 0 !important;

			.uni-card__content {
				padding: 0 !important;
			}
		}
	}

	.getScanCode {
		margin-right: 4px;
		margin-top: 10px;
	}

	::v-deep .is-disabled {
		color: black !important;
	}

	.triangle-facing-left {
		display: inline-block;
		border-left: 1px solid;
		border-bottom: 1px solid;
		width: 12px;
		height: 12px;
		transform: rotate(45deg);
	}
</style>